package com.pactera.asmp.server.controller.trace;

import com.alibaba.fastjson.JSON;
import com.pactera.asmp.server.common.annotations.JwtIgnore;
import com.pactera.asmp.server.common.constants.Result;
import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.utils.FileUtils;
import com.pactera.asmp.server.pojo.Issue;
import com.pactera.asmp.server.service.IssueService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ProjectName: asmpserver
 * @Package: com.pactera.asmp.server.controller.trace
 * @ClassName: IssueUploadController
 * @Author: lvzhiqiang
 * @Description: 客户端上传Issue数据 API控制类
 * @Date: 2019/8/27 15:20
 * @Version: 1.0
 */
@RestController
@RequestMapping("/issueUpload")
public class IssueUploadController {
    private static final Logger logger = LoggerFactory
            .getLogger(IssueUploadController.class);
    @Autowired
    private IssueService mIssueService;


    /**
     * @api {post} /issueUpload/uploadFile
     * @apiDescription Trace文件上传接口
     * @apiName uploadFile
     * @apiParam {MultipartFile} MultipartFile File
     * @apiParam {json} issue 描述和具体的功能字段
     * @apiParamExample {json} Request-Example:
     * {
     * "projectName": "K259泛亚项目",
     * "issueType": "system_app_anr",
     * "issueProcessName": "com.pacta.hmi.mymedia",
     * "vinCode": "lzzzzzzzzzzz",
     * "deviceVersion": "Delphi:20.10",
     * "androidVersion": "android7.0",
     * "issueCreateTime": "1566963056441",
     * "issueDescription": "CPU Test ANR"
     * }
     * @apiGroup Issue manage
     * @apiSuccessExample Success-Response:
     * {
     * "status": "200",
     * "message": "File upload sucess",
     * "data": null
     * }
     * @apiErrorExample Error-Response:
     * {
     * "status": "2040",
     * "message": "File upload failed",
     * "data": null
     * }
     * @apiSampleRequest /uploadFile
     */
    @PostMapping("/uploadFile")
    @JwtIgnore
    public @ResponseBody
    Result uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("issue") String issue) {
        logger.info("IssueUploadController uploadFile issue:>>  {}",issue);
        Issue newIssue = JSON.parseObject(issue, Issue.class);
        boolean status = mIssueService.addIssue(newIssue, file);
        String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
                .toUriString();
        Result<String> result = new Result(ResultCode.SUCCESS, fileDownloadUri);
        if (!status) {
            result.setCode(ResultCode.FAIL.code());
        }
        return result;
    }


    /**
     * @api {get} /issueUpload/download
     * @apiDescription Trace文件上传接口
     * @apiName uploadFile
     * @apiParam {String} tracePath trace文件所处路径
     * @apiGroup Issue manage
     * @apiSuccessExample Success-Response:
     * {
     * "status": "200",
     * "message": "Trace download sucess",
     * "data": null
     * }
     * @apiErrorExample Error-Response:
     * {
     * "status": "-200",
     * "message": "Trace download failed",
     * "data": null
     * }
     * @apiSampleRequest /uploadFile
     */
    @GetMapping("/download")
    @JwtIgnore
    public Result downloadFile(HttpServletRequest request, HttpServletResponse response, @RequestParam("tracePath") String path) {

        return FileUtils.dowloadFile(response, path);
    }

}
